///* submit 送出注册表单 */
//$("#modifyAD").click(function() {
//	EDIT_UpdateAdvment();
//});

$(document).ready(function() {
	$("#advVideoUrl").attr("src",addata.advVideoUrl);
	$("#advPhotoUrl").attr("src",addata.advPhotoUrl);
	$("#adCouponTitle").val(addata.advCouponTitle);
	$("#advCouponUrl").attr("src",addata.advCouponUrl);
	$("#adId").val(addata.advmentId);
	$("#adTitle").val(addata.advName);
	$("#adDescription").text(addata.advDesc);
//	$("#adKeyword").text(addata.advKeyword);
//	if(addata.advKeyword.split(" ")[0] !== ''){ $("#adKeyword1").val(addata.advKeyword.split(" ")[0]);}	
	for(i=0; i<10; i++){
		if(addata.advKeyword.split(" ")[i] !== ''){ $("#adKeyword"+(i+1)).val(addata.advKeyword.split(" ")[i]);}
	}
	$('input[name="adPosition"][value='+addata.advPosition+']').prop("checked",true);
	$('#adLocation [value='+addata.advLocId+']').prop("selected",true);
	$("#adStartTime").val(addata.advOnshtime);
	$("#adEndTime").val(addata.advOffshtime);
});

function EDIT_UpdateAdvment() {
	var postUrl = 'ModifyAdv';
	var tempKeyword = $('#adKeyword1').val();
	if($('#adKeyword2').val() !== ''){ tempKeyword += " "+$('#adKeyword2').val(); }
	if($('#adKeyword3').val() !== ''){ tempKeyword += " "+$('#adKeyword3').val(); }
	if($('#adKeyword4').val() !== ''){ tempKeyword += " "+$('#adKeyword4').val(); }
	if($('#adKeyword5').val() !== ''){ tempKeyword += " "+$('#adKeyword5').val(); }
	if($('#adKeyword6').val() !== ''){ tempKeyword += " "+$('#adKeyword6').val(); }
	if($('#adKeyword7').val() !== ''){ tempKeyword += " "+$('#adKeyword7').val(); }
	if($('#adKeyword8').val() !== ''){ tempKeyword += " "+$('#adKeyword8').val(); }
	if($('#adKeyword9').val() !== ''){ tempKeyword += " "+$('#adKeyword9').val(); }
	if($('#adKeyword10').val() !== ''){	tempKeyword += " "+$('#adKeyword10').val(); }
	var dataSend = {
		advmentId: $("#adId").val(),
		adTitle: $("#adTitle").val(),
		adDescription: $("#adDescription").val(),
		adKeyword: tempKeyword,
		adCouponTitle: $("#adCouponTitle").val(),
//		advVideoUrl: $("#advVideoUrl").attr("src"),
//		advPhotoUrl: $("#advPhotoUrl").attr("src"),		
//		advCouponUrl: $("#advCouponUrl").attr("src"),
//		advPosition: $('input[name="adPosition"]:checked').val(),
//		advLocation: $('#adLocation').val(),
		adStartTime: $("#adStartTime").val(),
		adStartTime2: $("#adStartTime2").val(),
		adEndTime: $("#adEndTime").val(),
		adEndTime2: $("#adEndTime2").val(),
	};
	$.ajax({
		url : postUrl,
		type : "post",
		dataType : "json",
		data : dataSend,
		success : function(data) {
			console.log("连接 Servlet 服务成功,"+ postUrl + " request status is " + data.status);
			if(data.status){
				console.log("success to EDIT");
				dialog("更新成功",2000);
				$(".formEdit").hide();
				$(".ModifySuccess").show();
			}else{
				console.log("failure to EDIT");
				dialog("资料错误",2000);
			}
		},
		error : function(jqXHR, textStatus, errorThrown) {
			dialog("网路连线异常",2000);
			console.log("return fail");
		}
	});
}